package com.linkhub.oms.admin.modules.logistic.rules.strategy.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.logistic.rules.domain.ChannelRuleMatchRequest;
import com.linkhub.oms.admin.modules.logistic.rules.strategy.ChannelRuleMatchStrategy;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.enums.logistic.LogisticAttributeLimitEnum;
import com.linkhub.oms.common.util.QtOptionalUtil;
import com.linkhub.oms.dao.mpservice.ILocalProductService;
import com.linkhub.oms.entity.LocalProductEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.linkhub.oms.common.enums.product.LocalProductLogisticsAttributeEnum.FRAGILE;
import static com.linkhub.oms.common.enums.product.LocalProductLogisticsAttributeEnum.MAGNETIC;
import static com.linkhub.oms.common.enums.product.LocalProductLogisticsAttributeEnum.PASTE;
import static com.linkhub.oms.common.enums.product.LocalProductLogisticsAttributeEnum.TEXTILES;

/**
 * @Description: MovingAttributeRuleLimit
 * @Author: echo
 * @Date: 2024/6/1 17:00
 * @Version: 1.0
 */
@Slf4j
@Service
@Order(value = 4)
public class MovingAttributeRuleLimit extends BaseChannelRuleMatchStrategy implements ChannelRuleMatchStrategy {

    @Resource
    private ILocalProductService localProductService;
    @Override
    public boolean match(ChannelRuleMatchRequest request) {

        List<String> localSkuIds = request.getOrderInfoDO().getLocalSkuIds();
        List<LocalProductEntity> products = localProductService.listBySkuIds(localSkuIds);
        if (CollUtil.isEmpty(products)) {
            log.info("MovingAttributeRuleLimit localSkuIds isEmpty >>>>>>>> ");
            return false;
        }

        String channelCountryKey = request.assemblyChannelCountryKey();
        LogisticAttributeLimitEnum logisticAttributeLimit = IEnumConvert.valueOf(channelCountryKey, LogisticAttributeLimitEnum.class);
        if (Objects.isNull(logisticAttributeLimit)) {
            log.info("MovingAttributeRuleLimit skip, key=[{}]", channelCountryKey);
            return true;
        }

        List<Integer> codes = logisticAttributeLimit.getCodes();
        List<Integer> checkList = Lists.newArrayList(MAGNETIC.getCode(), FRAGILE.getCode(), PASTE.getCode(), TEXTILES.getCode());
        CollUtil.addAll(checkList, codes);
        for (LocalProductEntity product : products) {
            //List<Integer> logisticsAttribute = product.getLogisticsAttribute();
            // 先取仓库复核的物流属性，如果没有则取商家填的物流属性
            List<Integer> logisticsAttribute = CollUtil.defaultIfEmpty(product.getLogisticsAttributeRecheck(),product.getLogisticsAttribute());
            if (CollUtil.isEmpty(logisticsAttribute)) {
                continue;
            }
            if (!CollUtil.containsAll(checkList, logisticsAttribute)) {
                return false;
            }
        }

        return true;
    }
}
